package com.larissa.android.quiz;

import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.larissa.android.quiz.databinding.ActivityCreateQuestionBinding;
import com.larissa.android.quiz.question.Question;
import com.larissa.android.quiz.question.QuestionRepository;

import java.util.List;

public class CreateQuestionActivity extends AppCompatActivity{

    private ActivityCreateQuestionBinding binding;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCreateQuestionBinding.inflate(getLayoutInflater());
        binding.btnCreate.setOnClickListener(v -> {
            List<Question> questions;
            String category, text, answer;
            if(binding.radioButtonGeo.isChecked())
            {
                category = Question.GEO;
                questions = QuestionRepository.getInstance().getGeoQuestions();
            }else if(binding.radioButtonMath.isChecked())
            {
                category = Question.MATH;
                questions = QuestionRepository.getInstance().getMathQuestions();
            }
            else {
                return;
            }
            text = binding.editText.getText().toString();
            answer = binding.editAnswer.getText().toString();
            Question question = new Question(text, answer, category);
            questions.add(question);
            QuestionRepository.getInstance().getQuestionsToBeInserted().add(question);
            Toast.makeText(this, "created", Toast.LENGTH_LONG).show();
            QuestionRepository.getInstance().saveQuestionsToDB();
            finish();
        });
        setContentView(binding.getRoot());
    }
}
